<?php
/**
 *
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         IndexController.php
 */
namespace Scshux\Backend\Controllers;
use Scshux\Utils\Captcha;
use Scshux\Mvc\SModel;
class LoginController extends  ControllerBase
{
	/**
	 * 登录
	 */
	public  function  indexAction()
	{

	}

	/**
	 * 验证登录
	 */
	public  function  dologinAction()
	{
		$result = array('status'=>'n','msg'=>'');

		$postData = $_POST;
		if(empty($postData['login']) || empty($postData['password']) || empty($postData['captcha'])){
			$result['msg'] = '参数错误,请检查!';
			$this->sendJson($result);
		}

		$vcode  =  $this->session->get('vcode',false,true);
		if(strtolower($postData['captcha']) != strtolower($vcode)){
			$result['msg']  = '验证码不正确!';
			$this->sendJson($result);
		}

		$userDB = new SModel('admin_user');
		$login  = addslashes($postData['login']);
		$user   = $userDB->getObj(sprintf(
				"username='%s' or realname='%s' or phone='%s' or email='%s'",
				$login,$login,$login,$login));

		if(empty($user)){
			$result['msg']  = '用户名不正确!';
			$this->sendJson($result);
		}

		if($user->password == md5(trim($postData['password']))){
			if ($user->is_active == 1)
			{
				$this->getCore()->auth->authenticate($user->user_id);
				$this->getCore()->auth->logLogin();
				$result['status'] = 'y';
				$this->sendJson($result);
			}
			else 
			{
				$result['msg']  = '用户已被禁止登录!';
				$this->sendJson($result);
			}
			

		}else{
			$result['msg']  = '密码不正确!';
			$this->sendJson($result);
		}
	}

	/**
	 * 登出
	 */
	public  function  logoutAction()
	{
		$this->getCore()->auth->clearAuth();
		$this->redirect('login');
	}


	/**
	 * 验证
	 */
	public  function  captchaAction()
	{
		$captchaObj = new Captcha();
		$captchaObj->Generate();
		$this->session->set('vcode',$captchaObj->getCode());
		exit;

	}

}
